package org.viettan.android.main.view;

import java.util.List;

import org.viettan.android.main.R;
import org.viettan.android.main.model.Article;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;

import com.google.inject.Inject;
import com.google.inject.Provider;

/**
 * This {@code ArticleAdapter} helps in display {@link Article}s onto a list in the UI.
 */
public class ArticleAdapter extends ArrayAdapter<Article> {
    private final List<Article> items;
    private final Activity activity;
    private final ViewUtility viewUtility;

    @Inject
    ArticleAdapter(Provider<ViewUtility> viewUtilityProvider, Activity activity,
	    int textViewResourceId,
	    List<Article> items) {
	super(activity, textViewResourceId, items);
	this.items = items;
	this.activity = activity;
	this.viewUtility = viewUtilityProvider.get();
    }

    @Override
    public View getView(int position, View rowView, ViewGroup parent) {
	if (rowView == null) {
	     LayoutInflater vi = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	    rowView = vi.inflate(R.layout.image_two_lines_row, null);
	} else {
	    viewUtility.setImage(activity, rowView, R.id.image, false, null);
	}
	Article article = items.get(position);
	viewUtility.setImage(activity, rowView, R.id.image, false, article.getImageLargeUrl());
	viewUtility.setText(rowView, R.id.title, article.getTitle());
	viewUtility.setText(rowView, R.id.subtitle, article.getIntro());
	return rowView;
    }
}
